package com.controller;

import com.dao.StorageDao;
import com.entity.Role;
import com.entity.Storage;
import com.service.StorageService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/sys/storageRecord")
public class StorageController {
    @Resource
    private StorageService storageService;

    @RequestMapping("/list")
    public ModelAndView finStorage(){
        ModelAndView modelAndView = new ModelAndView();
        List<Storage> list = storageService.finStorage();
        modelAndView.addObject("StorageList",list);
        modelAndView.setViewName("storage/list");
        return modelAndView;
    }

    @RequestMapping("/view")
    public String storageId(@RequestParam(required = true)int id, Model model, String name){
        Storage storage = storageService.storageId(id);
        model.addAttribute("storage",storage);
        if (name.equals("查看")) {
            return "storage/view";
        }else {
            return "storage/update";
        }
    }

    @RequestMapping("/update")
    public String updateStorage(Storage storage){
        int update = storageService.updateStorage(storage);
        if (update>0){
            return "frame";
        }else {
            return "storage/update";
        }

    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "storage/add";
    }

    @RequestMapping("/add")
    public String add(Storage storage){
        int add = storageService.insertStorage(storage);
        if (add>0){
            return "frame";
        }else {
            return "storage/update";
        }
    }

    @RequestMapping("/wher")
    public ModelAndView wher(String queryStorageCode,String queryStorageName){
        ModelAndView modelAndView = new ModelAndView();
        List<Storage> list = storageService.listStorage(queryStorageCode, queryStorageName);
        modelAndView.addObject("StorageList",list);
        modelAndView.setViewName("storage/list");
        return modelAndView;
    }

}
